import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import {
  getData
} from '../static/session/setForm'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    component: Home,
    children: [{
        path: '',
        component: () => import('../views/home/index')
      },
      {
        path: 'msbd',
        component: () => import('../views/msbd/index')
      },
      {
        path: 'test',
        component: () => import('../views/test/index')
      },
      {
        path: 'setting',
        component: () => import('../views/setting/index')
      },
      {
        path: 'detail/:id',
        name: 'Detail',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('../views/msbd/detail'),
        props: true
      }
    ]
  },
  {
    path: '/login',

    component: () => import('../views/setting/login')
  },
  {
    path: '/myCollection',
    component: () => import('../views/setting/myCollection')
  },
  {
    path: '/myHistory',
    component: () => import('../views/setting/myHistory')
  },
]

const router = new VueRouter({
  routes
})

let whiteList = ['/setting', '/login']

router.beforeEach((to, from, next) => {
  // if(to.name=='')
  // if (to.name =='Detail') {
  //   if (to.params.item) {
  //     next()
  //   } else {
  //     next('/msbd')
  //   }
  // } else {
  //   next()
  // }
  // console.log(to)
  // console.log(store.state.login.form)
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (store.state.login.form.id) {
      next()
    } else {
      next('/setting')
    }
  }
})

export default router